﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Qiniu.Http;
using Qiniu.Storage;
using Qiniu.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;


namespace Books.Brands
{
    public class BrandAppService : CrudAppService<Brand, BrandDto, int, QueryBrandDto, CreateUpdateBrandDto>
    {
        private IRepository<Brand, int> _repository;
        public BrandAppService(IRepository<Brand, int> repository) : base(repository)
        {
            _repository = repository;
        }
        protected override async Task<IQueryable<Brand>> CreateFilteredQueryAsync(QueryBrandDto input)
        {
            return (await base.CreateFilteredQueryAsync(input)).WhereIf(!string.IsNullOrEmpty(input.BrandName), c => c.BrandName.Contains(input.BrandName));
        }

       
        [HttpPost("Add")]
        public int Add(CreateUpdateBrandDto dt)
        {
            var obj = ObjectMapper.Map<CreateUpdateBrandDto,Brand>(dt);
            for (int i = 0; i < dt.Tu.Length; i++)
            {
                if (i==0)
                {
                    obj.Tu1 = dt.Tu[0];
                }
                if (i == 1)
                {
                    obj.Tu2 = dt.Tu[1];
                }
                if (i == 2)
                {
                    obj.Tu3 = dt.Tu[2];
                }
                
            }
            _repository.InsertAsync(obj);
            return 1;
        }
    }
}
